package com.konne.nightmare.FastPublicOpinion.utils;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;

import androidx.annotation.NonNull;
import androidx.annotation.Size;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;
import pub.devrel.easypermissions.PermissionRequest;


public class PermissionUtils {
    public static final int RC_LOCATION_PERM = 123;
    public static final int RC_PHONE_PERM = 124;
    public static final int RC_SDCARD_PERM = 125;
    public static final int RC_CAMORE_PERM = 126;
    public static final int RC_ALL_PERM = 127;
    private static String[] mLoactionPermission = {
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION
    };

    private static String[] mPhoneStatusPermission = {Manifest.permission.READ_PHONE_STATE};

    private static String[] mSdcardPermission = {
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
    };

    private static String[] mCamorePermission = {
            Manifest.permission.CAMERA
    };

    private static String[] mAllPermission = {
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.INTERNET
    };


    @AfterPermissionGranted(RC_LOCATION_PERM)
    public static void locationTask(Activity mContext) {
        if (hansPermission(mContext, RC_LOCATION_PERM)) {
            // Have permissions, do the thing!
//            Toast.makeText(mContext, "TODO: Location and Contacts things", Toast.LENGTH_LONG).show();
        } else {
            // Ask for both permissions
            requestPermissions(mContext, RC_LOCATION_PERM, mLoactionPermission);
        }
    }

    @AfterPermissionGranted(RC_PHONE_PERM)
    public static void phonePermissionTask(Activity mContext) {
        if (hansPermission(mContext, RC_PHONE_PERM)) {
            // Have permissions, do the thing!
//            Toast.makeText(mContext, "TODO: Location and Contacts things", Toast.LENGTH_LONG).show();
        } else {
            // Ask for both permissions
            requestPermissions(mContext, RC_PHONE_PERM, mPhoneStatusPermission);
        }
    }

    @AfterPermissionGranted(RC_SDCARD_PERM)
    public static void sdCardPermissionTask(Activity mContext) {
        if (hansPermission(mContext, RC_SDCARD_PERM)) {
            // Have permissions, do the thing!
//            Toast.makeText(mContext, "TODO: Location and Contacts things", Toast.LENGTH_LONG).show();
        } else {
            // Ask for both permissions
            requestPermissions(mContext, RC_SDCARD_PERM, mSdcardPermission);
        }
    }

    @AfterPermissionGranted(RC_CAMORE_PERM)
    public static void camorePermissionTask(Activity mContext) {
        if (hansPermission(mContext, RC_CAMORE_PERM)) {
            // Have permissions, do the thing!
//            Toast.makeText(mContext, "TODO: Location and Contacts things", Toast.LENGTH_LONG).show();
        } else {
            // Ask for both permissions
            requestPermissions(mContext, RC_CAMORE_PERM, mCamorePermission);
        }
    }

    @AfterPermissionGranted(RC_ALL_PERM)
    public static void allPermissionTask(Activity mContext) {
        if (hansPermission(mContext, RC_ALL_PERM)) {
            // Have permissions, do the thing!
//            Toast.makeText(mContext, "TODO: Location and Contacts things", Toast.LENGTH_LONG).show();
        } else {
            // Ask for both permissions
            requestPermissions(mContext, RC_ALL_PERM, mAllPermission);
        }
    }


    public static boolean hansPermission(Activity mContext, int code) {
        switch (code) {
            case RC_LOCATION_PERM:
                return EasyPermissions.hasPermissions(mContext, mLoactionPermission);

            case RC_PHONE_PERM:
                return EasyPermissions.hasPermissions(mContext, mPhoneStatusPermission);
            case RC_SDCARD_PERM:
                return EasyPermissions.hasPermissions(mContext, mSdcardPermission);
            case RC_CAMORE_PERM:
                return EasyPermissions.hasPermissions(mContext, mCamorePermission);
            case RC_ALL_PERM:
                return EasyPermissions.hasPermissions(mContext, mAllPermission);
        }
        return false;

    }


    @SuppressLint("RestrictedApi")
    public static void requestPermissions(@NonNull Activity host,
                                          int requestCode, @Size(min = 1) @NonNull String... perms) {
        // rational的值为空时，直接调用权限申请，绕过EasyPermission的判断
        PermissionRequest request = new PermissionRequest.Builder(host, requestCode, perms).build();
        request.getHelper().directRequestPermissions(requestCode, perms);

    }
}
